package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.LinkMapper;
import com.itheima.pojo.Link;
import com.itheima.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class LinkServiceImpl implements LinkService {

    @Autowired
    private LinkMapper linkMapper;

    @Override
    public void add(Link link) {

        linkMapper.insert(link);
    }

    @Override
    public void delete(Link link) {
    linkMapper.delete(link);
    }

    @Override
    public void edit(Link link) {
        Link link1 = linkMapper.selectByPrimaryKey(link.getUid());
        link.setCreateTime(link1.getCreateTime());
        link.setUpdateTime(new Date());
        linkMapper.delete(link1);
        linkMapper.insert(link);

    }

    @Override
    public Page<Link> getList(Link link, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(link);
        return (Page<Link>)linkMapper.selectByExample(example);
    }

    @Override
    public void stick(Link link) {
        String maxSort=linkMapper.getMaxSort();
        int sort = Integer.parseInt(maxSort);
        maxSort=String.valueOf(sort+1);
        String uid=link.getUid();
        linkMapper.stick(uid,maxSort);

    }

    //增加友情链接点击数
    @Override
    public void addLinkCount(String uid) {
        Link link = linkMapper.selectByPrimaryKey(uid);
        if (link !=null){
            link.setClickCount(link.getClickCount()+1);
            link.setUpdateTime(new Date());
            linkMapper.updateByPrimaryKeySelective(link);
        }

    }

    //获取友情链接
    @Override
    public List<Link> getLink() {
        List<Link> linkList = linkMapper.selectAll();
        return linkList;
    }

    @Override
    public Page<Link> getall(String currentPage, String pageSize, String keyword, String linkStatus) {
        PageHelper.startPage(Integer.parseInt(currentPage),Integer.parseInt(pageSize));


        return (Page<Link>)linkMapper.selectAll();
    }

    /**
     *
     * @param link
     * @return
     */
    private Example createExample(Link link){
        Example example=new Example(Link.class);
        Example.Criteria criteria = example.createCriteria();
        if(link!=null){
            // 友链标题
            if(link.getTitle()!=null && !"".equals(link.getTitle())){
                criteria.andLike("title","%"+link.getTitle()+"%");
            }
            // 友链状态
            if(link.getLinkStatus()!=null && !"".equals(link.getLinkStatus())){
                criteria.andEqualTo("linkStatus",link.getLinkStatus());
            }
            // 用户uid
            if(link.getUid()!=null && !"".equals(link.getUid())){
                criteria.andEqualTo("uid",link.getUid());
            }
            // 友链链接
            if(link.getUrl()!=null && !"".equals(link.getUrl())){
                criteria.andEqualTo("url",link.getUrl());
            }
            //友链介绍
            if(link.getSummary()!=null && !"".equals(link.getSummary())){
                criteria.andLike("summary","%"+link.getSummary()+"%");
            }

        }
        return example;
    }
}
